﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Xml.Xsl;
using System.IO;
using System.Xml;

namespace Winwise.SPWebConfig.Core.XslModification
{
    public class XSLWebConfigModification : SPWebConfig.Core.WebConfigModification
    {

        [Persisted]
        private string m_XslTransformation;

        [Persisted]
        private string m_XslReverseTransformation;


        public string XslTransformation
        {
            get
            {
                return m_XslTransformation;
            }
            set
            {
                m_XslTransformation = value.Trim();
            }
        }

        public string XslReverseTransformation
        {
            get
            {
                return m_XslReverseTransformation;
            }
            set
            {
                m_XslReverseTransformation = value.Trim();
            }
        }

        public override void ApplyModification(System.Xml.XmlDocument doc, Microsoft.SharePoint.Administration.SPUrlZone zone, Microsoft.SharePoint.Administration.SPWebApplication webapp)
        {
            XslCompiledTransform xslt = new XslCompiledTransform();
            using (StringReader sr = new StringReader(XslTransformation))
            {
                using (XmlReader reader = XmlReader.Create(sr))
                {
                    xslt.Load(reader);
                    using (StringReader srWebConfig = new StringReader(doc.OuterXml))
                    {
                        using (XmlReader readerWebConfig = XmlReader.Create(srWebConfig))
                        {
                            StringBuilder sb = new StringBuilder();

                            using (XmlWriter writer = XmlWriter.Create(sb))
                            {
                                xslt.Transform(readerWebConfig, writer);
                                doc.LoadXml(sb.ToString());
                            }
                        }
                    }
                }
            }
        }

        public override void RemoveModification(System.Xml.XmlDocument doc, Microsoft.SharePoint.Administration.SPUrlZone zone, Microsoft.SharePoint.Administration.SPWebApplication webapp)
        {
            XslCompiledTransform xslt = new XslCompiledTransform();
            using (StringReader sr = new StringReader(XslReverseTransformation))
            {
                using (XmlReader reader = XmlReader.Create(sr))
                {
                    xslt.Load(reader);

                    using (StringReader srWebConfig = new StringReader(doc.OuterXml))
                    {
                        using (XmlReader readerWebConfig = XmlReader.Create(srWebConfig))
                        {
                            StringBuilder sb = new StringBuilder();

                            using (XmlWriter writer = XmlWriter.Create(sb))
                            {
                                xslt.Transform(readerWebConfig, writer);
                                doc.LoadXml(sb.ToString());
                            }
                        }
                    }
                }
            }
        }

        public override string FriendlyTypeName
        {
            get { return "XSL Modification"; }
        }

        public override bool SupportEditing
        {
            get { return true; }
        }

        public override string AdminPage
        {
            get { return "Winwise.SPWebConfig\\AddEditXSLWebconfigModification.aspx"; }
        }

        public override void Validate()
        {
            try
            {

                XslCompiledTransform xslt = new XslCompiledTransform();
                using (StringReader sr = new StringReader(XslReverseTransformation))
                {
                    using (XmlReader reader = XmlReader.Create(sr))
                    {
                        xslt.Load(reader);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("XslReverseTransformation is not a valid XSLT : {0}", e.ToString()));
            }
            try
            {
                XslCompiledTransform xslt = new XslCompiledTransform();
                using (StringReader sr = new StringReader(XslTransformation))
                {
                    using (XmlReader reader = XmlReader.Create(sr))
                    {
                        xslt.Load(reader);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("XslTransformation is not a valid XSLT : {0}", e.ToString()));
            }
        }
    }
}
